class SubjectsController < ApplicationController
  layout 'educational_plans'
  
  # GET /subjects
  # GET /subjects.xml
  def index
    @subjects = Subject.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @subjects }
    end
  end

  def list
    @educational_plan = EducationalPlan.find(params[:plan_id])
    @duration = @educational_plan.duration_in_terms
    subjects = Subject.find(:all, :conditions =>
        ["educational_plan_id = ? AND (is_required OR (NOT is_required AND is_optional_stub))",
        @educational_plan.id],
      :order => 'term_number, name')
    opt_subjects = Subject.find(:all, :conditions =>
        ["educational_plan_id = ? AND NOT is_required AND NOT is_optional_stub",
        @educational_plan.id],
      :order => 'term_number, name')

    @row_count = subjects.size / @duration
    @row_count += 1 if subjects.size % @duration > 0

    @subject_list = Array.new(@duration)
    for i in 1..@duration
      @subject_list[i-1] = subjects.select { |s| s.term_number == i }
    end

    @opt_row_count = opt_subjects.size / @duration
    @opt_row_count += 1 if opt_subjects.size % @duration > 0
    @opt_subject_list = Array.new(@duration)
    for l in 1..@duration
      @opt_subject_list[l-1] = opt_subjects.select { |s| s.term_number == l }
    end

    respond_to do |format|
      format.html 
    end
  end

  # GET /subjects/1
  # GET /subjects/1.xml
  def show
    @subject = Subject.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @subject }
    end
  end

  def add_to_plan
    @subject = Subject.new
    @educational_plan = EducationalPlan.find(params[:plan_id])
    respond_to do |format|
      format.html { render :action => "new" }
      format.xml  { render :xml => @subject }
    end
  end

  # GET /subjects/new
  # GET /subjects/new.xml
  def new
    @subject = Subject.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @subject }
    end
  end

  # GET /subjects/1/edit
  def edit
    @subject = Subject.find(params[:id])
  end

  # POST /subjects
  # POST /subjects.xml
  def create
    @subject = Subject.new(params[:subject])

    respond_to do |format|
      if @subject.save
        flash[:notice] = 'Subject was successfully created.'
        format.html { redirect_to(@subject) }
        format.xml  { render :xml => @subject, :status => :created, :location => @subject }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @subject.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /subjects/1
  # PUT /subjects/1.xml
  def update
    @subject = Subject.find(params[:id])

    respond_to do |format|
      if @subject.update_attributes(params[:subject])
        flash[:notice] = 'Subject was successfully updated.'
        format.html { redirect_to(@subject) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @subject.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /subjects/1
  # DELETE /subjects/1.xml
  def destroy
    @subject = Subject.find(params[:id])
    @subject.destroy

    respond_to do |format|
      format.html { redirect_to(subjects_url) }
      format.xml  { head :ok }
    end
  end
end
